<?php

/*
 * Created on May 15, 2009
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Popup extends MY_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('popup_model', 'popup', true);
        $this->load->model('upload_files_model', 'file', true);
        $this->load->model('news_tags_model', 'news_tags', true);
        $this->load->model('tags_model', 'tags', true);
        $this->checkLogin();
        $this->load->helper('form');
        $this->template->set_template('admin');
        $this->buildMenu();
        
    }

    function Popup() {
        parent::__construct();
        $this->load->library('xajax', 'category');
        $this->xajax->registerFunction(array('test', &$this, 'test'));

        $this->xajax->processRequest();
        $this->xajax_js = $this->xajax->getJavascript($this->base_url . 'js/');
        $this->load->model('news_model', 'news', true);
        $this->checkLogin();
    }

    function index() {
        //echo $this->config->item('language');
        //$this->main = '<div> erer erer er </div>';
        //$this->execute();
        //$this->load->view('main', '');
        $this->view();
    }

    function create() {
        $obj = new stdClass();
        $data['popup_id'] = 0;
        $data['friendly_url'] = '';
        $data['id_ncategory'] = 0;
        $data['code_ncategory'] = '';
        $data['vi_name'] = '';
        $data['en_name'] = '';
        $data['vi_summary'] = '';
        $data['en_summary'] = '';
        $data['vi_description'] = '';
        $data['en_description'] = '';
        $data['active'] = 1;
        $data['image'] = '';
        $data['tags'] = '';
        $data['old_tags'] = '';
        $data['images'] = array();

        $data['date_add'] = date('Y-m-d H:i:s', time());
        $data['date_upd'] = date('Y-m-d H:i:s', time());

        $data['error_message'] = '';
        $data['legend_text'] = lang('create_news');
        
        $config['toolbar'] = array(
    array( 'Source','-',
          'NewPage','Preview','Templates','-',
          'Cut','Copy','Paste','PasteText','PasteFromWord','-',
          'Undo','Redo','-',
          'Find','Replace','-',
          'SelectAll','RemoveFormat','-',
          'Maximize', 'ShowBlocks'),
    '/',
    array('Bold','Italic','Underline','Strike','-',
          'Subscript','Superscript','-',
          'NumberedList','BulletedList','-',
          'Outdent','Indent','Blockquote','-',
          'JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock','-',
          'Link','Unlink','Anchor','-',
          'Image','Flash','Table','HorizontalRule','SpecialChar'
          ),
    '/',
    array('Format','Font','FontSize','-',
          'TextColor','BGColor', 'MediaEmbed')
);      
        
        $data['config'] = $config;
        $data['base_url'] = base_url();
        
        $this->load->library('ckeditor');
        $this->load->library('ckFinder');
        $this->template->add_js('asset/ckeditor/ckeditor.js');
        $this->template->add_js('asset/ckfinder/ckfinder.js');
        //configure base path of ckeditor folder
        $this->ckeditor->basePath = base_url() . 'asset/ckeditor/';
        $this->ckeditor->config['toolbar'] = 'Full';
        $this->ckeditor->config['language'] = 'en';
        //configure ckfinder with ckeditor config
        $this->ckfinder->SetupCKEditor($this->ckeditor, 'asset/ckfinder');


        //$this->template->parse_view('content', 'admin/product/create_product', $data);

        $this->template->parse_view('content', 'admin/popup/create_popup', $data);
        $this->template->render();
    }

    function view_file() {
        $data = array();
        $this->main = $this->load->view('admin/product/view_file', $data, true);
        $this->execute();
    }

    function edit($id) {
        $news = $this->popup->getById($id);
        $data = array();
        if (!$news) {
            redirect('admin/popup/view');
        }

        $data = $news;
        
        $data['error_message'] = '';
        $data['legend_text'] = lang('edit_news');
        
        $this->load->library('ckeditor');
        $this->load->library('ckFinder');
        $this->template->add_js('asset/ckeditor/ckeditor.js');
        $this->template->add_js('asset/ckfinder/ckfinder.js');
        //configure base path of ckeditor folder
        $this->ckeditor->basePath = base_url() . 'asset/ckeditor/';
        $this->ckeditor->config['toolbar'] = 'Full';
        $this->ckeditor->config['language'] = 'en';
        //configure ckfinder with ckeditor config
        $this->ckfinder->SetupCKEditor($this->ckeditor, 'asset/ckfinder');
        
        $config['toolbar'] = array(
    array( 'Source','-',
          'NewPage','Preview','Templates','-',
          'Cut','Copy','Paste','PasteText','PasteFromWord','-',
          'Undo','Redo','-',
          'Find','Replace','-',
          'SelectAll','RemoveFormat','-',
          'Maximize', 'ShowBlocks'),
    '/',
    array('Bold','Italic','Underline','Strike','-',
          'Subscript','Superscript','-',
          'NumberedList','BulletedList','-',
          'Outdent','Indent','Blockquote','-',
          'JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock','-',
          'Link','Unlink','Anchor','-',
          'Image','Flash','Table','HorizontalRule','SpecialChar'
          ),
    '/',
    array('Format','Font','FontSize','-',
          'TextColor','BGColor', 'MediaEmbed')
);      
        
        $data['config'] = $config;
        $data['base_url'] = base_url();
        $data['site_url'] = site_url();
        $data['images'] =  $this->file->getFileByType($id, IMAGE_TYPE, NEWS_TYPE);
        
        if(empty($data['images']))
                $data['images'] = array();
                
        $this->template->parse_view('content', 'admin/popup/create_popup', $data);
        $this->template->render();
        
    }

    function view() {

        $this->load->library('pagination');

        //$category = $this->uri->segment(4, -1);
        $page = (int) $this->uri->segment(4);
        $config['base_url'] = site_url('admin/news/view/');

        $config['total_rows'] = $this->popup->countAll();
        $config['per_page'] = NUMBER_RECORD_PER_PAGE;
        $config['uri_segment'] = 4;
        $config['first_link'] = 'First';

        $this->pagination->initialize($config);

        //$paging = $this->pagination->create_links();
        //$this->setTemplate(array ('admin/pcategory/pcategory_list.tpl'));
        //$items = $this->loadItems();
        //$items = '';
        //$pcategories = $this->pcategory->getAllByCode($code_parent, NUMBER_RECORD_PER_PAGE, $page);
        //$paging = $this->pagination->create_links();
        //$this->setTemplate(array ('admin/news/news_list.tpl'));
        //$items = $this->loadItems();
        $items = '';
        $news = $this->popup->getAll($page, NUMBER_RECORD_PER_PAGE);
        /* $this->setTemplate(array ('admin/news/news_item.tpl'));
          for($i = 0; $i < count($products); $i++){
          $id = $products[$i]->id_news;
          $name = $products[$i]->vi_name;
          $description = $products[$i]->cat_name;
          $items .= eval ($this->loadTemplate('admin/news/news_item.tpl'));
          } */

        //$this->main = eval ($this->loadTemplate('admin/news/news_list.tpl'));
        //$this->execute();
        $data['rows'] = $news;
        $this->template->parse_view('content', 'admin/popup/popup_list', $data);
        $this->template->render();
    }

    function loadItems() {
        /* $item = '';
          $items = $this->pcategory->getAll();
          $this->setTemplate(array ('admin/pcategory/pcategory_item.tpl'));
          for($i = 0; $i < count($items); $i++){
          $id = $items[$i]->id_pcategory;
          $name = $items[$i]->vi_name;
          $description = $items[$i]->vi_description;
          $item .= eval ($this->loadTemplate('admin/pcategory/pcategory_item.tpl'));
          }

          return $item; */
    }

    function loadNewsItems($categories = null, $parent = 0, $track = array(), $style = 1, $index = 1) {
        $items = '';
        if (!$categories) {
            $categories = $this->category->getAll();
        }
        $tmp = array();
        for ($i = 0; $i < count($categories); $i++) {
            $category = $categories[$i];
            $tmp[$category->category_parent_id][$category->category_id] = $category;
        }

        if (isset($tmp[$parent])) {
            $num_record = count($tmp[$parent]);
            foreach ($tmp[$parent] as $k => $v) {
                $parent = $v->category_id;
                $category_id = $v->category_id;
                $category_name = $v->name_vi;
                if ($index % 2 == 0)
                    $class = 'class="bg"';
                else
                    $class = '';
                $items .= eval($this->loadTemplate('news_categories_item.tpl'));
                //if(in_array($v->category_id, $track))
                $index++;
                $items .=$this->loadProductItems($categories, $parent, $track, $style + 1, $index);
            }

            /* for($j = 1; $j <= count($tmp[$parent]); $j++){
              if(isset($tmp[$parent][$j])){
              $category = $tmp[$parent][$j];
              $parent = $category->category_id;
              $category_id = $category->category_id;
              $category_name = $category->name;
              if($index%2 == 0) $class = 'class="bg"';
              else $class = '';
              $items .= eval($this->loadTemplate('product_categories_item.tpl'));
              //if(in_array($v->category_id, $track))
              $index++;
              $items .=$this->loadProductItems($categories , $parent, $track, $style + 1, $index);
              }
              } */
        }
        //print_r($categories);
        return $items;
    }

    function buidDDNewsCategoryList($name, $default) {
        $this->load->model('ncategory_model', 'ncategory', true);
        $categories = $this->ncategory->getAll();
        //$this->setTemplate(array('admin/option_ddl.tpl', 'admin/ddl.tpl'));
        $space = '';
        //$name = 'Parent';
        $value = '0';
        $selected = '';
        $space = '';
        $label = lang('select_category');
        $class = '';
        $action = '';
        $style = '';

        $tmp = array(); //print_r($categories);
        for ($i = 0; $i < count($categories); $i++) {
            $category = $categories[$i];
            //$tmp[$category->id_parent_pcategory][$category->id_pcategory] = $category;
            $tmp[$category->id_parent_ncategory][$category->id_ncategory] = $category;
        }

        $options = $this->load->view('admin/option_select', array('value' => $value, 'selected' => $selected, 'space' => $space, 'label' => $label), true) . $this->getCategoryOption($tmp/* $categories */, 0, $default);
        $data = array();
        $data['name'] = $name;
        $data['style'] = $style;
        $data['class'] = $class;
        $data['action'] = $action;
        $data['options'] = $options;
        return $this->load->view('admin/select', $data, true);
        //$options = eval($this->loadTemplate('admin/option_ddl.tpl')) . $this->getCategoryOption($tmp/* $categories */, 0, $default);
        //return eval($this->loadTemplate('admin/ddl.tpl'));
    }

    function getCategoryOption($tmp/* $categories */, $parent = 0, $default = 0, $space = '&nbsp;') {
        $items = '';
        if (isset($tmp[$parent])) {
            $num_record = count($tmp[$parent]);
            foreach ($tmp[$parent] as $k => $v) {
                $parent = $v->id_ncategory;
                $value = $v->id_ncategory;
                $label = $v->vi_name;
                $selected = '';
                if ($value == $default)
                    $selected = 'selected';
                
                $items .= $this->load->view('admin/option_select', array('value' => $value, 'selected' => $selected, 'space' => $space, 'label' => $label), true);
                $items .=$this->getCategoryOption($tmp/* $categories */, $parent, $default, $space . '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');

                //$items .= eval($this->loadTemplate('admin/option_ddl.tpl'));
                //$items .=$this->getCategoryOption($tmp/* $categories */, $parent, $default, $space . '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
            }
        }

        return $items;
    }

    function save() {
    	
        $txtID = (int) trim($this->input->post('txtID'));
        $txtFriendlyUrl = trim($this->input->post('txtFriendlyUrl'));

        $data = array();
        if ($txtID > 0){
            $news = $this->popup->getById($txtID);

            if (empty($news)) {
                redirect('admin/popup/view');
                return;
            }

            $data = $news;
            $data['images'] =  $this->file->getFileByType($txtID, IMAGE_TYPE, NEWS_TYPE);
        }
        else {
            $data['id_news'] = 0;
            $data['friendly_url'] = '';
            $data['image'] = '';
            
            $images = $this->session->userdata('images');
            if(empty ($images))
                $images = array();
            $data['images'] = $images;
            $image = $this->session->userdata('image_default');
            if(empty ($image))
                $image = '';
            $data['image'] = $image;
            
        }
	
        $this->load->helper(array('form', 'url'));

        $this->load->library('form_validation');

        //$this->form_validation->set_rules('category', 'lang:category', 'callback_select_category_check');
        //$this->form_validation->set_message('code_check', '%s is not available.');
        $this->form_validation->set_rules('txtViName', 'lang:vi_name', 'required|min_length[2]');
        //$this->form_validation->set_rules('txtEnName', 'lang:en_name', 'required|min_length[2]');
        //$this->form_validation->set_rules('txtViSummary', 'lang:vi_summary', 'required|min_length[2]');
        //$this->form_validation->set_rules('txtEnSummary', 'lang:en_summary', 'required|min_length[2]');
        $this->form_validation->set_rules('txtViDescription', 'lang:vi_description', 'required|min_length[2]');
        //$this->form_validation->set_rules('txtEnDescription', 'lang:en_description', 'required|min_length[2]');
        //if($txtLinkRewrite!='' AND $txtLinkRewrite!=$category->link_rewrite)
        //$this->form_validation->set_rules('txtLinkRewrite', 'lang:friendly_url', 'callback_friendly_url_check');
        /* $this->form_validation->set_rules('password', 'Password', 'required');
          $this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
          $this->form_validation->set_rules('email', 'Email', 'required');
         */

        //$data['friendly_url'] = trim($this->input->post('txtFriendlyUrl'));
        //$data['id_ncategory'] = trim($this->input->post('category'));
        $data['vi_name'] = trim($this->input->post('txtViName'));
        $data['en_name'] = trim($this->input->post('txtEnName'));
        $data['vi_summary'] = trim($this->input->post('txtViSummary'));
        $data['en_summary'] = trim($this->input->post('txtEnSummary'));
        $data['vi_description'] = trim($this->input->post('txtViDescription'));
        $data['en_description'] = trim($this->input->post('txtEnDescription'));
        //$data['tags'] = trim($this->input->post('txtTag'));
        //  $data['old_tags'] = trim($this->input->post('txtOldTag'));
        $data['active'] = (int) $this->input->post('active');
        //$product->image = '';
        $data['date_upd'] = date('Y-m-d H:i:s', time());
        $error_message = ''; // $this->loadErrorMessage('Thong diep loi.');
        //print_r($_POST);
        $attach_image = false;
        $image_upload = array('error' => false);
        if (isset($_FILES['file']['tmp_name']) AND strlen($_FILES['file']['tmp_name']) > 0) {
            $attach_image = true;
            $image_upload = $this->upload_image('file', NEWS_IMAGE_PATH /* './Data/Images/Product/' */);
            if ($image_upload['error'])
                $error_message .= $image_upload['message'];
        }

        /* if($attach_image == true AND $image_upload['error']==true){
          echo 'Not is image'; return;
          } */

        if ($this->form_validation->run() == FALSE OR ($attach_image == true AND $image_upload['error'] == true)) {

            $error_message = validation_errors() . '<br>' . $error_message;
            //$code = uniqid();
            //$this->session->set_userdata('code', $code);

           // $data = array();
          //  $data['news'] = $news;
          //  $data['ddlCategory'] = $this->buidDDNewsCategoryList('category', $data['id_ncategory']);

            $data['error_message'] = '<div style="color:red">' . $error_message . '</div>';
            //if()
            $data['legend_text'] = $txtID > 0 ? lang('edit_news') : lang('create_news');
            
            $config['toolbar'] = array(
    array( 'Source','-',
          'NewPage','Preview','Templates','-',
          'Cut','Copy','Paste','PasteText','PasteFromWord','-',
          'Undo','Redo','-',
          'Find','Replace','-',
          'SelectAll','RemoveFormat','-',
          'Maximize', 'ShowBlocks'),
    '/',
    array('Bold','Italic','Underline','Strike','-',
          'Subscript','Superscript','-',
          'NumberedList','BulletedList','-',
          'Outdent','Indent','Blockquote','-',
          'JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock','-',
          'Link','Unlink','Anchor','-',
          'Image','Flash','Table','HorizontalRule','SpecialChar'
          ),
    '/',
    array('Format','Font','FontSize','-',
          'TextColor','BGColor')
);      
        
        $data['config'] = $config;
        $data['base_url'] = base_url();
        
            $this->load->library('ckeditor');
            $this->load->library('ckFinder');
            $this->template->add_js('asset/ckeditor/ckeditor.js');
            $this->template->add_js('asset/ckfinder/ckfinder.js');
            //configure base path of ckeditor folder
            $this->ckeditor->basePath = base_url() . 'asset/ckeditor/';
            $this->ckeditor->config['toolbar'] = 'Full';
            $this->ckeditor->config['language'] = 'en';
            //configure ckfinder with ckeditor config
            $this->ckfinder->SetupCKEditor($this->ckeditor, 'asset/ckfinder');
            $this->template->parse_view('content', 'admin/popup/create_popup', $data);
            $this->template->render();

            
            return;
        } else {
            //$this->load->model('pcategory_model', 'pcategory', true);
            //$cat = $this->pcategory->getByCode($product->code_pcategory);
            //$product->id_pcategory = $cat->id_pcategory;
            
			
            $result = 0;
            $insert_id = 0;
            if ($txtID > 0) {
                $result = $this->popup->update($data);
            } else {
				
                $data['date_add'] = date('Y-m-d H:i:s', time());
                $txtID = $insert_id = $result = $this->popup->create($data);
                
            }
           

            $images = $this->session->userdata('images');

            if ($insert_id > 0) {
                if(!empty($images) && count($images) > 0){

                $_images = array();
                for ($i = 0; $i < count($images); $i++) {
                    $name_image = $images[$i];
                    $image = new stdClass();
                    $image->item_id = $result;
                    $image->vi_file_name = $name_image['vi_file_name'];
                    $image->en_file_name = $name_image['vi_file_name'];
                    $image->vi_alias = '';
                    $image->en_alias = '';
                    $image->vi_description = '';
                    $image->en_description = '';
                    $image->position = $this->file->getMaxPosition() + 1;
                    $image->file_type = IMAGE_TYPE;
                    $image->item_type = NEWS_TYPE;
                    $image->active = 1;
                    $image->add_date = date('Y-m-d H:i:s', time());
                    $image->upd_date = date('Y-m-d H:i:s', time());

                    $_images[] = $image;


                    }

                if (count($_images) > 0) {

                    $this->file->create($_images);

                }
                $this->session->unset_userdata('images');
                }
                
            }
            

           redirect('admin/popup/view');
        }
    }
    
    function do_upload($consultant_id)
    {
        //$this->session->unset_userdata('images');
        $uploaddir = NEWS_IMAGE_PATH; //'./uploads/';

        $config['file_name'] =  time(). basename($_FILES['uploadfile']['name']); //date('Y-m-d H:i:s', time()).basename($_FILES['uploadfile']['name']);
        $config['upload_path'] = $uploaddir;
        $config['allowed_types'] = 'gif|jpg|png|jpeg';
            //$config['overwrite'] = true;
            $config['max_size'] = 8192; // 8192 kb = 8mb
            
            $result_upload = $this->uploadFile($config);
            if ($result_upload['error']) {

                //$error_message = $result_upload['message'];
               //echo "error";
               // echo $result_upload['message'];
                $data['error'] = true;
                //$data['cname'] = $info['cname'];
            } else if($result_upload['error'] == false){
                if($consultant_id == 0){
                    $images = $this->session->userdata('images');

                    $images[]['vi_file_name'] = $config['file_name'];

                    $this->session->set_userdata('images', $images);
                    $data['error'] = false;
                    $data['filename'] = $config['file_name'];
                    $data['image_id'] = time();
                }
                else{

                    $image = new stdClass();
                    $image->item_id = $consultant_id;
                    $image->vi_file_name = $config['file_name'];
                    $image->en_file_name = $config['file_name'];
                    $image->vi_alias = '';
                    $image->en_alias = '';
                    $image->vi_description = '';
                    $image->en_description = '';
                    $image->position = $this->file->getMaxPosition() + 1;
                    $image->file_type = IMAGE_TYPE;
                    $image->item_type = NEWS_TYPE; // 2:consultant, 1:portfolio
                    $image->active = 1;
                    $image->add_date = date('Y-m-d H:i:s', time());
                    $image->upd_date = date('Y-m-d H:i:s', time());

                    $image_id = $this->file->create($image);
                    //$this->session->set_userdata('images', $images);
                    $data['error'] = false;
                    $data['filename'] = $config['file_name'];
                    $data['image_id'] = $image_id;
                }

                //echo $config['file_name']; //"success";

            }

            echo json_encode($data);

    }

    function uploadFile($config) {

        $this->load->library('upload', $config);
        $result = array('error' => false);

        if (!$this->upload->do_upload('uploadfile')) {
            $result['message'] = $this->upload->display_errors();
            $result['error'] = true;
        } else {
            $result['upload_data'] = $this->upload->data();
        }

        return $result;
    }
    
    function delete_image(){
        $imageName = $this->input->post('image_name');
        $consultantID = $this->input->post('news_id');
        $result['error'] = false;

        if($consultantID > 0){
            $_resutl = $this->file->deleteByName($imageName);

            $consultant  = $this->news->getById($consultantID);
            if(!empty ($consultant) ){
                if($consultant['image'] == $imageName)
                    $this->news->setImage('', $consultantID);

            }

            if($_resutl){
                 $result['error'] = false;
            }
            else
             $result['error'] = true;



        }
        else{
            $result['error'] = false;
            $images = $this->session->userdata('images');
            for($i = 0; $i < count($images); $i++){
                if($images[$i] == $imageName ){
                    unset($images[$i]);
                    $image_default = $this->session->userdata('image_default');
                    if( $image_default == $imageName)
                        $this->session->unset_userdata('image_default');
                    break;
                }
            }
            $this->session->set_userdata('images', $images);

        }

        @unlink(NEWS_IMAGE_PATH.$imageName);

        echo json_encode($result);
    }

    
    function set_image(){
        $imageName = $this->input->post('image_name');
        $consultantID = $this->input->post('news_id');
        if($consultantID > 0){
            $resutl = $this->news->setImage($imageName, $consultantID);
            if($resutl){
                 $result['error'] = false;
            }
             $result['error'] = true;
        }
        else{
            $result['error'] = false;
            $this->session->set_userdata('image_default', $imageName);
        }

         echo json_encode($result);
    }

    function upload_image($field_name, $path) {
        $result = array();
        $config['upload_path'] = $path; //'./Data/Images/';
        $config['allowed_types'] = 'gif|jpg|png|jpeg';
        //$config['max_size']	= '1000';
        //$config['max_width']  = '1024';
        //$config['max_height']  = '768';
        $config['overwrite'] = true;

        $this->load->library('upload', $config);

        if (!$this->upload->do_upload($field_name)) {
            //$error = array('error' => $this->upload->display_errors());
            $result['error'] = true;
            $result['message'] = $this->upload->display_errors();
            //$this->load->view('upload_form', $error);
        } else {
            //$data = array('upload_data' => $this->upload->data());
            $result['error'] = false;
            $result['upload_data'] = $this->upload->data();
        }

        return $result;
    }

    function code_check($code) {
        $result = $this->product->getByCode($code);
        if ($result != null) {
            $this->form_validation->set_message('code_check', '%s is not available.');
            return FALSE;
        } else {
            return TRUE;
        }
    }

    function friendly_url_check($url) {
        $result = $this->news->getByFriendlyUrl($url);
        if ($result != null) {
            $this->form_validation->set_message('friendly_url_check', '%s is not available.');
            return FALSE;
        } else {
            return TRUE;
        }
    }

    function select_category_check($cat_id) {

        if (intval($cat_id) == 0) {
            $this->form_validation->set_message('select_category_check', lang('none_selected_category'));
            return FALSE;
        } else {
            return TRUE;
        }
    }

    function testGetFile() {
        $config = array(
            'allowed' => array('JPEG', 'jpg', 'PNG', 'png', 'GIF', 'gif'),
            'max_size' => 1024000000 /* 1Mb */
        );

        $result = getFile($config, 'Data', 'http://images8.dantri.com.vn/ThumbImages/Uploaded/2009/05/08/lut080520094_327.jpg');

        if (!$result['error'])
            echo 'Get file success.';
        else
            echo $result['message'];
    }

    function delete() {
        $id = intval($this->uri->segment(4));
        $ids = array();
        if ($id > 0) {
            $ids[] = $id;
            //$this->product->delete($id);
        } else {
            if (($this->input->post('submitDelcategory'))) {
                $ids = $this->input->post('categoryBox');
            }
        }

        if (count($ids)) {
            // Xoa file anh chinh:
            $this->load->model('fck_files_model', 'fckfile', true);
            $newses = $this->news->getAllByIds($ids);
            $main_images = array();
            $fck_images = array();
            $fck_images_id = array();
            if (count($newses) > 0) {
                for ($i = 0; $i < count($newses); $i++) {
                    $main_images[] = $newses[$i]->image;
                    $_fck_files = $this->fckfile->getAllByItemId($newses[$i]->id_news, 'news');
                    if (count($_fck_files) > 0) {
                        for ($j = 0; $j < count($_fck_files); $j++) {
                            $fck_images[] = $_fck_files[$j]->file_url;
                            $fck_images_id[] = $_fck_files[$j]->id;
                        }
                    }
                }
            }


            //Xoa san pham
            $result = $this->news->delete($ids);
            if ($result) {
                if (count($main_images) > 0) {
                    for ($i = 0; $i < count($main_images); $i++) {
                        @unlink(NEWS_IMAGE_PATH.$main_images[$i]);
                    }
                }

                if (count($fck_images) > 0) {
                    for ($i = 0; $i < count($fck_images); $i++) {
                        @unlink($fck_images[$i]);
                    }
                }
                if (count($fck_images_id) > 0)
                    $this->fckfile->delete($fck_images_id);
            }
        }

        redirect('admin/news');
    }

}

?>
